﻿using System.Collections.Specialized;
using NL.ADA.Common;

namespace NL.ADA.ConfigProxy
{
    public static class ConfigurationManager
    {
        static NameValueCollection _appSettings = GetAppSettings(Proxy.Instance.Storage.Config);
        public static NameValueCollection AppSettings
        {
            get
            {
                return _appSettings;
            }
        }

        private static NameValueCollection GetAppSettings(CustomConfigList list)
        {
            var result = new NameValueCollection();
            foreach (CustomConfig config in list)
            {
                result.Add(config.Key,config.Value);
            }
            return result;
        }
        
        internal static void Refresh()
        {
            _appSettings = GetAppSettings(Proxy.Instance.Storage.Config);
        }

        public static void Disconnect()
        {
            Proxy.Instance.Dispose();
        }

    }

    public static class ConfigurationManager<T>
    {
        static KeyValueTypeCollection<T> _appSettings2 = GetAppSettings(Proxy.Instance.Storage.Config);

        public static KeyValueTypeCollection<T> AppSettings
        {
            get
            {
                return _appSettings2;
            }
        }

        public static KeyValueTypeCollection<T> GetAppSettings(CustomConfigList list)
        {
            var result = new KeyValueTypeCollection<T>();
            foreach (var config in list)
            {
                result.Add(config.Key, new KeyValueType(config.Value, CustomConfig.GetTypeFromConfigTypeId(config.ConfigTypeId)));

            }
            return result;
        }

        public static void Refresh()
        {
            _appSettings2 = GetAppSettings(Proxy.Instance.Storage.Config);
        }

    }
}
